package com.ruoyi.login;



import com.ruoyi.exception.BizException;
import com.ruoyi.utils.DingDingUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


//获取access_token
//用AppKey和AppSecret换取access_token
@Component
public class AccessTokenCache {
    private static Logger logger = LoggerFactory.getLogger(AccessTokenCache.class);

    private static AccessTokenCache cache = new AccessTokenCache();
    private String appToken = null;

    public AccessTokenCache() {
        }

        public static AccessTokenCache getInstance() {
            return cache;
        }

        public void init() {
            while (true) {
                try {
                    appToken = DingDingUtil.getAccessToken("dingviqxqtok5nvnpuqt", "M3lKSPkX3yC3l7ZcNSZyayXX6cXbaifOxEhvkIuUfsLLhewxqOPfnE9XmjzqkyyS");
                    if (null != appToken) {
                        logger.info("获取token成功， appToken:{}", appToken);
                        // 休眠 1 小时
                        Thread.sleep(3600 * 1000);
                    } else {
                        // 如果access_token为null，60秒后再获取
                        Thread.sleep(60 * 1000);
                    }
                } catch (InterruptedException e) {
                    try {
                        Thread.sleep(60 * 1000);
                    } catch (InterruptedException e1) {
                        logger.error("{}", e1);
                    }
                    logger.error("{}", e);
                } catch (BizException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        public String getAppToken() {
            return appToken;
        }
    }



